package com.jwdf.uda.update;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.jwdf.uda.R;
import com.jwdf.uda.event.UserEvent;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.view.dialog.ConfirmDialog;
import com.umeng.message.PushAgent;

class UpdateDialog {

    static void show(final Context context, String content, final String downloadUrl, final UpdateVersionCallback callback) {
        if(isContextValid(context)) {
            ConfirmDialog dlg = new ConfirmDialog(context, context.getString(R.string.android_auto_update_dialog_title), content, context.getString(R.string.android_auto_update_dialog_btn_cancel), context.getString(R.string.android_auto_update_dialog_btn_download), new ConfirmDialog.CallbackListener() {
                @Override
                public void clickNo() {
                    if(callback != null) {
                        callback.onNext();
                    }
                }

                @Override
                public void clickYes() {
                    goToDownload(context, downloadUrl);
                    if(callback != null) {
                        callback.onNext();
                    }
                }
            });
            dlg.setCanceledOnTouchOutside(false);
            dlg.setCancelable(false);
            dlg.setCanBackDismiss(false);
            dlg.show();
        }
    }
    private static boolean isContextValid(Context context) {
        return context instanceof Activity && !((Activity) context).isFinishing();
    }
    private static void goToDownload(Context context, String downloadUrl) {
        Intent intent = new Intent(context.getApplicationContext(), DownloadService.class);
        intent.putExtra(Constants.APK_DOWNLOAD_URL, downloadUrl);
        context.startService(intent);
    }
    static void showForceDialog(final Context context, final String content, final String downloadUrl, final UpdateVersionCallback callback) {
        if(isContextValid(context)) {
            ConfirmDialog dlg = new ConfirmDialog(context, context.getString(R.string.android_auto_update_dialog_title), content, context.getString(R.string.android_force_update_dialog_btn_cancel), context.getString(R.string.android_auto_update_dialog_btn_download), new ConfirmDialog.CallbackListener() {
                @Override
                public void clickNo() {
                    doForceClose(context);
                }

                @Override
                public void clickYes() {
                    goToDownload(context, downloadUrl);
                    if(callback != null) {
                        doForceClose(context);
                    }
                }
            });
            dlg.setCanceledOnTouchOutside(false);
            dlg.setCancelable(false);
            dlg.setCanBackDismiss(false);
            dlg.show();
        }
    }
    private static void doForceClose(Context context) {
        ConfigUtils.removeCurrentUser(context);
        ConfigUtils.setHttpHeadersCookie(context, "");
        PushAgent.getInstance(context).disable();
        UserEvent event = new UserEvent();
        event.setEvent(UserEvent.EVENT_LOGOUT);
        event.setExtra(context.getString(R.string.android_force_update_dialog_btn_cancel));
        EventBusUtil.post(event);
    }
}
